package com.yifi.system.service;

import cn.hutool.core.util.StrUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.base.BaseService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 姚善利
 * @classname com.yifi.system.service
 * @description     用户权限
 * @date 2022/12/16 9:16
 */
public class UserLimitService extends BaseService {

    /**
     *  用户菜单权限
     * @param uid
     * @return
     */
    public Set<String> getPermissions(String uid)
    {
        String sql="select cpost from mis_user_post where ccode='"+uid+"'";
        sql="select t2.permission from mis_post_menu t1,work t2 where t1.cwork=t2.ccode " +
                " and (t1.cpost in ("+sql+") or t1.cpost='all')";
        List<Record> list= Db.find(sql);
        Set<String> permsSet = new HashSet<>();
        for (Record record:list) {
            String permission=record.getStr("permission");
            if(StrUtil.isNotEmpty(permission))
            {
                permsSet.add(permission);
            }
        }
        return permsSet;
    }

    public Set<String> getRoles(String uid)
    {
        String sql="select cpost from mis_user_post where ccode='"+uid+"'";
        List<Record> list= Db.find(sql);
        Set<String> roleSet = new HashSet<>();
        for (Record record:list) {
            String cpost=record.getStr("cpost");
            if(StrUtil.isNotEmpty(cpost))
            {
                roleSet.add(cpost);
            }
        }
        return roleSet;
    }
}
